//
//  File.swift
//  YYProject
//
//  Created by 陆文宇 on 2017/8/31.
//  Copyright © 2017年 陆文宇. All rights reserved.
//

import Foundation

final class YYLocation: NSObject, BMKLocationServiceDelegate {
    
    static let shared = YYLocation()
    
    var locService: BMKLocationService
    var completionHandler: ((Bool) -> Void)?
    var cityId = -1
    
    override init() {
        locService = BMKLocationService()
        super.init()
    }
    
    static func startLocation(_ completionHandler: ((Bool) -> Void)? = nil) {
        shared.completionHandler = completionHandler
        shared.locService.delegate = shared
        shared.locService.startUserLocationService()
    }
    
    static func getLocation() -> (lat: String, lng: String) {
        let location = shared.locService.userLocation.location
        if location != nil {
            return ("\(location!.coordinate.latitude)", "\(location!.coordinate.longitude)")
        }
        return ("", "")
    }
    
    static func getLocationCity() -> YYCity? {
        return YYCity.getOne(id: shared.cityId)
    }
    
    func didUpdate(_ userLocation: BMKUserLocation!) {
        var cityId = -1
        if userLocation != nil {
            yy_Log("didUpdateUserLocation lat:\(userLocation.location.coordinate.latitude) lon:\(userLocation.location.coordinate.longitude)")
            locService.stopUserLocationService()
            let newlocation = userLocation.location!
            
            let geocoder =  CLGeocoder()
            geocoder.reverseGeocodeLocation(newlocation, completionHandler: { (clplacemarks : [CLPlacemark]?, error : Error?) in
                
                if (clplacemarks != nil) {
                    if clplacemarks!.count > 0 {
                        let placemark = (clplacemarks?[0])! as CLPlacemark
                        
                        if let localCity = YYCity.getOne(name: placemark.locality) {
                            if let localSubcity = YYCity.getOne(name: placemark.subLocality, parentId: localCity.id) {
                                cityId = localSubcity.id
                            } else {
                                cityId = localCity.id
                            }
                        } else if let localCity = YYCity.getOne(name: placemark.administrativeArea) {
                            cityId = localCity.id
                        } else {
                            cityId = 0
                        }
                        
                        self.cityId = cityId
                        
                        self.completionHandler?(true)
                        self.completionHandler = nil
                    }
                }
            })
        } else {
            self.cityId = -1
        }
    }
    
    func didFailToLocateUserWithError(_ error: Error!) {
        yy_Log("didFailToLocateUserWithError:" + error.localizedDescription)
        locService.stopUserLocationService()
        cityId = -1
        
        self.completionHandler?(false)
        self.completionHandler = nil
    }
    
}

